package com.example.mysx2;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class InfoAdapter extends RecyclerView.Adapter<InfoAdapter.ViewHolder> {
    private final List<Student> data;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName, tvStudentId;

        public ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvStudentId = itemView.findViewById(R.id.tvStudentId);
        }
    }

    public InfoAdapter(List<Student> data) {
        this.data = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Student student = data.get(position);
        holder.tvName.setText(student.getName());
        holder.tvStudentId.setText(student.getStudentId());

        holder.itemView.setOnClickListener(v -> {
            String info = "姓名：" + student.getName() + "\n学号：" + student.getStudentId();
            Toast.makeText(v.getContext(), info, Toast.LENGTH_SHORT).show();
            Log.d("STUDENT_INFO", info);
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
